package com.lyz.moil.core.security.action;

import com.lyz.moil.core.orm.PageData;
import com.lyz.moil.core.security.entity.ApiDoc;
import com.lyz.moil.core.security.entity.DepartmentType;
import com.lyz.moil.core.security.service.ApiDocService;
import com.lyz.moil.core.security.service.DepartmentTypeService;
import com.lyz.moil.core.utils.WebUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/security/apidoc")
public class ApiDocAction {
    @Resource
    private ApiDocService apiDocService;

    @RequestMapping("/list")
    @ResponseBody
    public List<ApiDoc> list(HttpServletRequest request) {
        return apiDocService.findAll();
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public void save(ApiDoc apiDoc) {
        if (apiDoc.isNew()) {
            apiDocService.save(apiDoc);
        } else {
            apiDocService.update(apiDoc);
        }
    }

    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable("id") Integer id) {
        apiDocService.delete(id);
    }

    @InitBinder
    public void InitBinder(WebDataBinder dataBinder) {
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

}
